package main

import (
	"flag"
	"fmt"
	"log"

	"go.uber.org/zap"

	"gin-scaffold/dao/mysql"
	"gin-scaffold/dao/redis"
	"gin-scaffold/logger"
	"gin-scaffold/router"
	"gin-scaffold/setting"
)

var profilePath = flag.String("profile_path", "./conf/config.yaml", "配置文件路径")

func main() {
	flag.Parse()

	// 1. 加载配置
	err := setting.Init(*profilePath)
	if err != nil {
		log.Fatal(err)
	}

	// 2. 初始化日志模块
	err = logger.Init()
	if err != nil {
		log.Fatalf("cannot init logger; err: %v\n", err)
	}
	defer func() {
		_ = zap.L().Sync()
	}()

	// 3. 数据库初始化
	err = mysql.Init()
	if err != nil {
		zap.L().Fatal("cannot init mysql", zap.Error(err))
	}

	// 4. redis初始化
	err = redis.Init()
	if err != nil {
		zap.L().Error("cannot init redis", zap.Error(err))
	}

	// 5. 路由初始化
	r := router.Setup()

	zap.L().Info("server start", zap.Int("port", setting.Conf.Port))
	if err := r.Run(fmt.Sprintf(":%d", setting.Conf.Port)); err != nil {
		zap.L().Fatal("start failed", zap.Error(err))
	}
}
